<template>
  <div class="zy-content pb50 addAndDetail">
    <back-level></back-level>
    <!--      <Modal :mask-closable="false" v-model="changesVisible" :title="!id ? `${$t('ledger.NewMotion')}` : isDetail ? `${$t('ledger.CheckMotion')}` : `${$t('ledger.EditMotion')}`" :fullscreen="true"-->
    <!--             class="addModal">-->
    <Form ref="formChanges" :model="formInline" :disabled="isDetail" :rules="validateChangeFunction" label-position="top">
      <Card :title="$t('ledger.AlienDevice')" class="detail-content-layout">
        <div class="modalTitle tab mb16 mr16" v-show="!isDetail" style="display: inline-block">
          <Button type="primary" ghost cirle  @click="handleAddDevice">{{ $t('ledger.NewDevice') }}</Button>
        </div>
        <Button class="" type="primary"  @click="handleMulDelete"
                v-show="selectData.length" style="display: inline-block">  {{ $t('common.batchDelete') }}
        </Button>
        <div class="tableContent">
          <Table :columns="columns" height="400" :data="dataSource" @on-selection-change="handleDeviceSelect">
            <template slot-scope="{row}" slot="newOrgId">
              <Cascader  :data="orgList" :value="row.newOrg" :transfer="true"
                        @on-change="handleCasChange($event, row)"></Cascader>
            </template>
            <template slot-scope="{ row }" slot="action">
              <span class="operationBtn" @click="handleDelete(row)" v-show="!isDetail">{{ $t('common.Delete') }}</span>
            </template>
          </Table>
        </div>
      </Card>
      <Card :title="$t('common.ReasonForCancellation')" v-if="formInline.applyStatus==1" class="detail-content-layout mt16">
        <div class="modalTitle">
          <!--          <span> {{// $t('common.ReasonForCancellation')}}</span>-->
          <div style="margin:20px">
            <Input v-model="formInline.backReason" type="textarea" :rows="3" disabled style="width:30%"/>
          </div>
        </div>
      </Card>
      <Card :title="$t('ledger.RelocationType')" class="detail-content-layout mt16">
        <!--        <div class="modalTitle">-->
        <!--          <span>{{$t('ledger.RelocationType')}}</span>-->
        <div>

          <!--          </div>-->
          <!-- <Tooltip placement="right" max-width="350">
              <Icon type="ios-help-circle-outline" size="24"/>
              <div slot="content">
                  <p>转售/租用：调入公司与调出公司需不一致</p>
                  <p>调拨：公司一致，成本中心需不一致</p>
                  <p>借用：公司不一致；成本中心不一致</p>
                  <p>仅记录：公司与成本中心都需一致</p>
                  <p>委外生产借用：调入公司手动输入，调入成本中心非必填</p>
                  <p>借用归还：公司不一致；成本中心不一致</p>
              </div>
          </Tooltip> -->
          <div class="label">
           <Row :gutter="24">
             <Col :span="12">
               <FormItem :label="$t('ledger.RelocationType')" class="cycle" prop="ydlx">
                 <RadioGroup v-model="formInline.ydlx" @on-change="handleChangeType">
                   <Radio label="1">转售</Radio>
                   <Radio label="3">租用</Radio>
                   <Radio label="2">调拨</Radio>
                   <Radio label="4">借用</Radio>
                   <Radio label="7">借用归还</Radio>
                   <Radio label="5">仅记录</Radio>
                   <Radio label="6">委外生产借用</Radio>
                 </RadioGroup>
               </FormItem>
             </Col>
             <Col :span="6">
               <FormItem :label="$t('ledger.ApplicationReason')" class="cycle item" prop="applyRemark">
                 <Input style="width: 300px;" type="text" v-model="formInline.applyRemark" :placeholder="`${$t('common.inputInfo')}`"
                        class="inputBorder" :maxlength="32"></Input>
               </FormItem>
             </Col>
           </Row>
          </div>
        </div>
      </Card>
      <Card :title="$t('ledger.Location')" class="detail-content-layout mt16">
        <div class="modalTitle">
          <!--          <span>{{$t('ledger.Location')}}</span>-->
          <div class="label">
            <Row :gutter="24">
              <Col :span="6">
                <FormItem :label="$t('ledger.TransactOutCompany')" class="item">
                  <!--                            {{ formInline.outFactoryName }}-->
                  <Input style="width: 300px"
                         type="text"
                         v-model="formInline.outFactoryName"
                         class="inputBorder"
                         disabled>
                  </Input>
                </FormItem>
              </Col>
              <Col :span="6">
                <FormItem :label="$t('ledger.TransactOutPosition')" class="item">
                  <Cascader style="width: 300px"
                            disabled
                            clearable
                            :transfer="true"
                            :data="orgList"
                            placeholder=""
                            v-model="formInline.oldOrgId"
                            change-on-select></Cascader>
                </FormItem>
              </Col>
              <Col :span="6">
                <FormItem :label="$t('调出成本中心')" class="item">
<!--                  <Select v-model="formInline.moveOutCostCenter"
                          style="width: 300px"
                          filterable
                          clearable
                          disabled
                          placeholder=""
                          :remote-method="getCenterList"
                          :loading="loading">
                    <Option v-for="(item,index) of centerList"
                            :value="item.costCenter"
                            :key="index"
                            :label="item.ktextName"/>
                  </Select>-->
                  <Input style="width: 300px"
                         type="text"
                         v-model="formInline.costCenter"
                         class="inputBorder"
                         disabled>
                  </Input>
                </FormItem>
              </Col>
              <Col :span="6">
                <FormItem class="item" :label="$t('ledger.TransactInCompany')" prop="newBelongCompanyId"
                          >
                  <Select v-model="formInline.newBelongCompanyId"
                          clearable
                          :disabled="isDetail"
                          :placeholder="`${$t('common.selectInfo')}`"
                          style="width: 300px; text-align:left;"
                          @on-change="handleNewChange">
                    <Option v-for="(item,index) of factoryList" :value="item.id" :key="index" :label="item.name"></Option>
                  </Select>
                </FormItem>
              </Col>
              <Col :span="6">
                <FormItem :label="$t('ledger.TransactInPosition')" label-position="top" class="item" prop="newOrgIds">
                  <Cascader v-model="formInline.newOrgIds" style="width:300px" :data="orgList"
                            :transfer="true" @on-change="handleCasChangenew"></Cascader>
                </FormItem>
              </Col>
              <Col :span="6">
                <FormItem :label="$t('调入成本中心')" class="item" prop="moveInCostCenter">
                  <Select v-model="formInline.moveInCostCenter"
                          style="width: 300px"
                          filterable
                          clearable
                          :placeholder="$t('common.inputInfo')"
                          :remote-method="getCenterList"
                          @on-select="moveInCostCenterSelect"
                          :loading="loading">
                    <Option v-for="(item,index) of centerList"
                            :value="item.costCenter"
                            :key="index"
                            :tag="item.useDept"
                            :label="item.ktextName"/>
                  </Select>
                </FormItem>
              </Col>
            </Row>
          </div>
        </div>
      </Card>
      <Card :title="$t('ledger.OwnedBy')" class="detail-content-layout mt16">
        <div class="modalTitle">
          <!--          <span>{{$t('ledger.OwnedBy')}}</span>-->
          <div class="label">


            <Row :gutter="24">
              <Col :span="6">
                <FormItem :label="$t('ledger.CurrentHandler')" class="item" prop="moveOutAdminNoShowName"
                          style="margin-right: 20px;width:300px;">
                  <!--                            <Input v-model="formInline.moveOutAdminNoShowName"  style="width: 200px" disabled />-->
                  <Select v-model="formInline.moveOutAdminNoShowName" filterable clearable
                          :placeholder="`${$t('common.inputInfo')}`" :remote-method="getOutOAList" @on-select="moveSelect" :loading="loading">
                    <Option v-for="(item,index) of outOAList" :value="item.showName" :key="index" :tag="item.id" :label="item.showName"/>
                  </Select>
                </FormItem>
              </Col>
              <Col :span="6">
                <FormItem :label="$t('ledger.SuccessiveHandler')" class="item " prop="moveInAdminNoShowName"
                          style="margin-right: 20px;width:300px;">
                  <Select v-model="formInline.moveInAdminNoShowName" filterable clearable @on-select="moveInSelect"
                          :placeholder="`${$t('common.inputInfo')}`" :remote-method="getInOAList" :loading="loading">
                    <Option v-for="(item,index) of inOAList" :value="item.showName" :key="index" :tag="item.id" :label="item.showName"/>
                  </Select>
                </FormItem>
              </Col>
            </Row>

          </div>
        </div>
      </Card>
    </Form>
    <!--        <div class="addProject">-->
    <!--          <Form ref="formChanges" :model="formInline" :disabled="isDetail" :rules="validateChangeFunction">-->
    <!--           -->
    <!--            -->
    <!--            -->
    <!--            -->
    <!--           -->
    <!--          </Form>-->
    <!--        </div>-->
    <!--        {{ formInline }}-->
    <!--        <div slot="footer">-->
    <!--        -->
    <!--        </div>-->
    <changes-device-modal ref="changeDeviceModal" @handleMulDevice="handleMulDevice"/>
    <!--设置功能位置-->
    <position-modal ref="positionModal" :orgList="orgList" @getPositionValue="getPositionValue"/>
    <bottom-handle-wrap>
      <Button type="default" @click="handleCancel">{{ $t('common.Cancle') }}</Button>
      <!--            <Button type="primary" v-show="!isDetail" :loading="saveLoading" @click="handleOk(0)">{{$t('common.Save')}}</Button>-->
      <Button type="primary" v-show="!isDetail" :loading="loadingVisible" @click="handleOk(1)">
        {{ $t('common.submit') }}
      </Button>
    </bottom-handle-wrap>
    <!--      </Modal>-->
  </div>
</template>

<script>
import recursion from '../../../../util/recursion'
import changesDeviceModal from './changesDeviceModal'
import PositionModal from "./positionModal";
import {findOrgByToken} from '@/api/index.js'

export default {
  name: "AddChangesModal",
  components: {
    PositionModal,
    changesDeviceModal
  },
  data() {
    return {
      id: '',
      isDetail: false,
      loading: false,
      saveLoading: false,
      loadingVisible: false,
      changesVisible: false,
      isRequired1: false,
      isRequired2: false,
      isRequired3: false,
      isRequired4: false,
      formInline: {
        ydlx: '1',
        applyRemark: '',
        newBelongCompanyId: '',
        newBelongCompanyName: '',
        moveInCostCenterName: '',
        moveInCostCenter: '',
        ktextName: '',
        applyUserNoShowName: '',
        moveOutAdminNoShowName: '',
        moveInAdminNoShowName: '',
        oldOrgId: null,
        newOrgIds: null,
      },
      validateChangeFunction: {
        ydlx: [
          {required: true, message: '请选择异动类型', trigger: 'blur'}
        ],
        applyRemark: [
          {required: true, message: '请输入申请事由', trigger: 'blur'}
        ],
        newBelongCompanyId: [
          {required: true, message: '请选择调入公司', trigger: 'change'}
        ],
        newBelongCompanyName: [
          {required: true, message: '请输入调入公司', trigger: 'blur'}
        ],
        newOrgIds: [
          {required: true, type:'array', message: '请输入调入位置', trigger: 'change'}
        ],
        moveInCostCenter: [
          {required: true, type:'string', message: '请输入调入成本中心', trigger: 'change'}
        ],
        applyUserNoShowName: [
          {required: true, message: '请输入申请人', trigger: 'change'}
        ],
        moveOutAdminNoShowName: [
          {required: true, message: '请输入调出设备管理员', trigger: 'change'}
        ],
        moveInAdminNoShowName: [
          {required: true, message: '请输入调入设备管理员', trigger: 'change'}
        ],
      },
      classification: [],
      category: [],
      position: [],
      columns: [
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
        {
          title: `${this.$t('common.index')}`,
          key: '',
          align: 'center',
          minWidth: 80,
          render: (h, params) => {
            // console.log(params.index + 1)
            return h('div', params.index + 1)
          }
        },
        {
          title: `${this.$t('equipCheck.SerialNo')}`,
          key: 'deviceMId',
          align: 'center',
          minWidth: 160
        },
        {
          title: `${this.$t('equipCheck.AssetNo')}`,
          key: 'assetsNo',
          align: 'center',
          minWidth: 160
        },
        {
          title: `${this.$t('maintenance.EquipmentNo')}`,
          key: 'deviceNo',
          align: 'center',
          minWidth: 140
        },
        {
          title: `${this.$t('设备等级')}`,
          key: 'categoryName',
          align: 'center',
          minWidth: 120
        },
        {
          title: `${this.$t('maintenance.EquipmentName')}`,
          key: 'deviceName',
          align: 'center',
          minWidth: 120
        },
        {
          title: `${this.$t('equipCheck.Model')}`,
          key: 'specification',
          align: 'center',
          minWidth: 120,
        },
        {
          title: `${this.$t('ledger.CurrentPosition')}`,
          key: 'position4',
          align: 'center',
          minWidth: 250,
        },
        {
          title: `${this.$t('preliminary.Entity')}`,
          key: 'belongCompanyName',
          align: 'center',
          minWidth: 250,
        },
        // {
        //     title: '异动后安装位置',
        //     slot: 'newOrgId',
        //     align: 'center',
        //     minWidth: 300,
        // },
        // {
        //     title: '到厂日期',
        //     key: 'finalAcceptDate',
        //     align: 'center',
        //     minWidth: 100,
        // },
        {
          title: `${this.$t('common.operation')}`,
          key: '',
          align: 'center',
          slot: 'action',
          minWidth: 120,
        },
      ],
      orgList: [],
      dataSource: [],
      OAList: [],
      outOAList: [],
      inOAList: [],
      centerList: [],
      selectData: [],//选中的数据
      factoryList:[],
      selectDevicePrams:{}
    }
  },
  props: {
    // factoryList: {
    //   type: Array,
    //   default: () => []
    // },
    // orgList: {
    //     type: Array,
    //     default: () => []
    // }
  },
  created() {
    this.show(this.$route.query.handleType, this.$route.query, this.$route.query.detail)
    this.getOrgList()
    this.getCompanyList()
  },
  methods: {
    moveInCostCenterSelect(val){
      this.formInline.moveInCostCenterName = val.tag;
    },
    moveSelect(val){
      this.formInline.moveOutAdminId = val.tag
    },
    moveInSelect(val){
      console.log(val)
      this.formInline.moveInAdminId = val.tag
    },
    getCompanyList() {
      this.axios({
        method: 'get',
        url: '/basic/company/getCompanyListByUser',
        params: {}
      }).then(res => {
        if (res.code === 200) {
          let arr = res.data.map(item => {
            return {
              ...item,
              id: item.id.toString()
            }
          })
          this.factoryList = arr
        } else {
          this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
        }
      })
    },
    getCenterList(query) {
      if (query !== '') {
        this.axios({
          method: 'get',
          url: '/dms/dmsDevice/getCostCenter',
          params: {
            costCenter: query
          }
        }).then(res => {
          if (res.code === 200) {
            this.centerList = res.data.map(center => {
              return {
                ...center,
                ktextName: `${center.costCenter}-${center.useDept}`
              }
            })
            this.costCenter = query
          } else {
            this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
          }
        })
      } else {
        this.centerList = []
      }
    },
    getOrgList() {
      let params = {level: 3};
      findOrgByToken(params).then(res => {
        if (res.code === 200) {
          const obj = {
            label: 'name',
            value: 'id',
            children: 'children'
          }
          this.orgList = recursion.convertTree(res.data, obj)
          this.baseOrgList = res.data;
        } else {
          this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
        }
      })
    },
    /***********基础数据获取**************/
    getApplicationOAList(query) {
      // console.log(query)
      if (query !== '') {
        this.loading = true
        // if (this.formInline.applyUserNo && query !== this.formInline.applyUserNo) {
        //     // this.formInline.applyUserNo = ''
        //     return false
        // }
        // this.formInline.applyUserName = query.substr(13, query.length-1)
        this.axios({
          method: 'get',
          url: '/basic/user/searchOAUser',
          params: {
            key: query
          }
        }).then(res => {
          if (res.code === 200) {
            this.OAList = res.data.map(item => {
              return {
                ...item,
                showName: item.empno + '----' + item.username
              }
            })
          } else {
            this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
          }
        }).finally(() => {
          this.loading = false
        })
      } else {
        this.OAList = []
      }

    },
    getOutOAList(query,id) {
      if (query !== '') {
        if(id){
          this.formInline.moveOutAdminNoShowName = ''
          this.formInline.moveOutAdminId = ""
        }
        this.loading = true
        this.axios({
          method: 'get',
          url: '/basic/user/searchOAUser',
          params: {
            key: query
          }
        }).then(res => {
          if (res.code === 200) {
            this.outOAList = res.data.map(item => {
              return {
                ...item,
                showName: item.empno + '----' + item.username
              }
            })

            if (id){
              const obj = this.outOAList.find(item=> item.id == id)
              this.formInline.moveOutAdminNoShowName = obj.showName
              this.formInline.moveOutAdminId = obj.id
            }
          } else {
            this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
          }
        }).finally(() => {
          this.loading = false
        })
      } else {
        this.OAList = []
      }
    },
    getInOAList(query) {
      if (query !== '') {
        this.loading = true
        // if (this.formInline.moveInAdminNo && query !== this.formInline.moveInAdminNo) {
        //     // this.formInline.moveInAdminNo = ''
        //     return false
        // }
        // this.formInline.moveInAdminName = query.substr(13, query.length-1)
        let params = {}
        if (this.formInline.ydlx == 1 || this.formInline.ydlx == 2 || this.formInline.ydlx == 3 || this.formInline.ydlx == 4 || this.formInline.ydlx == 7) {
          params = {
            company: this.formInline.newBelongCompanyId,
            costCenter: this.formInline.ktextName ? this.formInline.ktextName.split('-')[0] : '',
            key: query
          }
        } else {
          params = {
            key: query
          }
        }
        this.axios({
          method: 'get',
          // url: '/dms/dmsUserCostCenter/costCenterUserlist',
          url: '/basic/user/searchUser',
          params
        }).then(res => {
          if (res.code === 200) {
            this.inOAList = res.data.map(item => {
              return {
                ...item,
                showName: item.empno + '----' + item.realname
              }
            })
          } else {
            this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
          }
        }).finally(() => {
          this.loading = false
        })
      } else {
        this.OAList = []
      }
    },
    /*******************页面按钮操作**************************/
    async show(type, record, detail) {
      this.changesVisible = true
      if (type === 'add') {
        // this.formInline = {}
        this.dataSource = []
        this.id = ''
        this.isDetail = false
        this.$nextTick(() => {
          this.$refs.formChanges.resetFields()
          this.formInline.ydlx = '1'
          this.formInline.newBelongCompanyId = ''
          this.formInline.outFactoryName = ''
          this.formInline.outCenterName = ''
          this.formInline.oldOrgId = null
          this.formInline.newOrgIds = null
        })
      } else {
        this.id = record.id
        this.isDetail = detail || false
        await this.getDeviceDetail(record.id)
        await this.getChangeDetail(record.id)
      }

    },
    //设备选择事件
    handleDeviceSelect(selection) {
      // console.log(selection)
      let arr = []
      selection.map(item => arr.push(item.id))
      this.selectData = arr
    },
    //设备批量删除
    handleMulDelete() {
      this.dataSource = this.dataSource.filter(item => this.selectData.indexOf(item.id) == -1)
      // console.log(this.selectData)
      this.selectData = []
    },
    //设备批量设置功能位置
    handleMulPosition() {
      this.$refs.positionModal.show()
    },
    //获取功能位置
    getPositionValue(arr) {
      // console.log(arr)
      this.dataSource.map(item => {
        if (this.selectData.includes(item.id)) {
          item.newOrg = arr
          item.newOrgId = arr[arr.length - 1]
        }
      })
      this.selectData = []
    },
    handleDelete(row) {
      this.dataSource = this.dataSource.filter(item => item.id != row.id)
      this.formInline.outFactoryName = ''
      this.formInline.outCenterName = ''
    },
    handleNewChange(val) {
      // console.log(val)
      this.formInline.ktextName = ''
    },
    handleChangeType(value) {
      // console.log(value)
      if (value === 2 || value === 5) {
        this.formInline.newBelongCompanyName = this.formInline.outFactoryName
        this.formInline.newBelongCompanyId = this.formInline.oldBelongCompanyId
      }
      if (value === 5) {
        this.formInline.moveInCostCenterName = this.formInline.outCenterName
      }
      if (value === 6) {
        this.formInline.newBelongCompanyName = ''
        this.formInline.moveInCostCenterName = ''
      }
    },
    //获取设备详情
    getDeviceDetail(id) {
      this.axios({
        method: 'post',
        url: '/dms/dmsMoveApply/getDmsMoveDetail',
        params: {
          id
        }
      }).then(res => {
        if (res.code === 200) {
          this.dataSource = res.data.map(item => {
            return {
              ...item,
              orgId: item.oldOrgId,
              orgName: item.oldPosition,
              newOrgId: item.newOrgId,
              newOrg: recursion.treeFindPath(this.orgList, node => node.value === item.newOrgId)
            }
          })
          // console.log(this.dataSource)
        } else {
          this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
        }
      }).finally(() => {
      })
    },
    //获取详情
    getChangeDetail(id) {
      this.axios({
        method: 'post',
        url: '/dms/dmsMoveApply/getDmsMoveApply',
        params: {
          id
        }
      }).then(res => {
        if (res.code === 200) {
          this.formInline = res.data
          this.formInline.newOrgId = res.data.newOrgIds
          this.formInline.oldOrgId = res.data.oldOrgIds
          this.formInline.moveInCostCenterName = res.data.ydlx == 5 ? res.data.moveInCostCenter + '-' + res.data.moveInCostCenterName : res.data.moveInCostCenterName
          this.formInline.newBelongCompanyId = res.data.newBelongCompanyId ? res.data.newBelongCompanyId.toString() : ''
          this.formInline.outFactoryName = res.data.oldBelongCompanyName
          this.formInline.costCenter = res.data.moveOutCostCenter + '-' + res.data.moveOutCostCenterName
          this.formInline.ktextName = res.data.moveInCostCenter + '-' + res.data.moveInCostCenterName
          this.formInline.applyUserNoShowName = res.data.applyUserNo + '----' + res.data.applyUserName
          this.formInline.moveInAdminNoShowName = res.data.moveInAdminNo + '----' + res.data.moveInAdminName
          //this.formInline.moveInAdminNoShowName = res.data.empno+ '----'+res.data.realname
          this.formInline.moveOutAdminNoShowName = res.data.moveOutAdminNo + '----' + res.data.moveOutAdminName
          // this.factoryList.filter(fac => fac.id === res.data.newBelongCompanyId).map(name => {
          //     this.formInline.companyCode = name.code
          // })
          this.getApplicationOAList(res.data.applyUserNo)
          this.getInOAList(res.data.moveInAdminName)
          this.getOutOAList(res.data.moveOutAdminName)
          this.getCenterList()

          // console.log("this.formInline", this.formInline)

        } else {
          this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
        }
      }).finally(() => {
      })

    },
    handleCasChangenew(val, selectedData) {
      this.formInline.newOrgId = val[val.length - 1]
    },
    handleCasChange(e, row) {
      // console.log(e, row)
      this.dataSource[row._index].newOrg = e
      this.dataSource[row._index].newOrgId = e[e.length - 1]
    },
    handleAddDevice() {
      this.$refs.changeDeviceModal.show(this.selectDevicePrams,this.dataSource.length)
    },
    handleMulDevice(data, obj) {

      // console.log(data, obj)
      let oldData = data.map(item => {
        return {
          ...item,
          newOrg: [],
          newOrgId: '',
        }
      })

      if (this.dataSource.length) {
        let newData = [...this.dataSource].filter(x => [...oldData].some(y => y.deviceMId === x.deviceMId))
        // console.log(newData)
        if (newData.length) {
          this.$Message.warning(({content: '有重复数据，请重新选择！', duration: 6, closable: true}));
          return false
        }
        this.dataSource = [...this.dataSource, ...oldData]
      } else {
        this.dataSource = [...this.dataSource, ...oldData]
      }

      this.factoryList.filter(fac => fac.id === obj.useCompanyId).map(item => {
        this.formInline.outFactoryName = item.name
        this.formInline.oldBelongCompanyId = item.id
      })

      this.formInline.moveOutCostCenter = obj.costCenter
      this.formInline.moveOutCostCenterName = obj.costCenterName
      this.formInline.costCenter = this.formInline.moveOutCostCenter + '-' + this.formInline.moveOutCostCenterName
      this.formInline.oldOrgId = obj.orgId

      const _obj = this.dataSource[this.dataSource.length-1]

      this.selectDevicePrams = {
        orgId:obj.realOrgId || [],
        useCompanyId: obj.useCompanyId || '',
        code:obj.orgId || '',
        costCenter: obj.costCenter
      }


      this.getOutOAList(_obj.equipmentManager, _obj.equipmentManagerId)
    },
    handleCancel() {
      // this.changesVisible = false
      // this.$refs.formChanges.resetFields()
      // this.formInline.outCenterName = ''
      // this.formInline.outFactoryName = ''
      // this.dataSource = []
      this.$router.go(-1)
    },
    handleOk(status) {
      let count = 0
      // if (this.formInline.ydlx == 1 || this.formInline.ydlx == 3 || this.formInline.ydlx == 4 || this.formInline.ydlx == 7) {
      //     if (this.formInline.newBelongCompanyId == this.formInline.oldBelongCompanyId) {
      //         this.$Message.warning(({content: '调入公司不得与调出公司一样！！', duration: 6, closable: true}))
      //         return false
      //     }
      // }
      // if (this.formInline.ydlx == 1 || this.formInline.ydlx == 2 || this.formInline.ydlx == 3 || this.formInline.ydlx == 4 || this.formInline.ydlx == 7) {
      //     if (this.formInline.outCenterName == this.formInline.ktextName) {
      //         this.$Message.warning(({content: '调入成本中心不得与调出成本中心一样！！', duration: 6, closable: true}))
      //         return false
      //     }
      // }
      if (!this.dataSource.length) {
        this.$Message.warning(({content: '请选择设备！', duration: 6, closable: true}))
        return false
      }
      // console.log(this.dataSource)
      // this.dataSource.map(item => {
      //     if (!item.newOrgId) {
      //         count = count + 1
      //     }
      // })
      // if (count) {
      //     this.$Message.warning(({content: '请选择异动位置！', duration: 6, closable: true}))
      //     return false
      // }

      let newBelongCompanyName = ''
      this.factoryList.map(item => {
        if (item.id == this.formInline.newBelongCompanyId) {
          newBelongCompanyName = item.name
        }
      })

      let applyUserNo = this.formInline.applyUserNoShowName ? this.formInline.applyUserNoShowName.split('----')[0] : ''
      let applyUserName = this.formInline.applyUserNoShowName ? this.formInline.applyUserNoShowName.split('----')[1] : ''
      let moveInAdminNo = this.formInline.moveInAdminNoShowName ? this.formInline.moveInAdminNoShowName.split('----')[0] : ''
      let moveInAdminName = this.formInline.moveInAdminNoShowName ? this.formInline.moveInAdminNoShowName.split('----')[1] : ''
      let moveOutAdminNo = this.formInline.moveOutAdminNoShowName ? this.formInline.moveOutAdminNoShowName.split('----')[0] : ''
      let moveOutAdminName = this.formInline.moveOutAdminNoShowName ? this.formInline.moveOutAdminNoShowName.split('----')[1] : ''



      // let applyUserName = this.OAList.length ? this.OAList.filter(item => item.empno == this.formInline.applyUserNo)[0].username : ''
      // let moveInAdminName = this.inOAList.length ? this.inOAList.filter(item => item.empno == this.formInline.moveInAdminNo)[0].username : ''
      // let moveOutAdminName = this.outOAList.length ? this.outOAList.filter(item => item.empno == this.formInline.moveOutAdminNo)[0].username : ''
      // console.log(newBelongCompanyName)
      this.$refs.formChanges.validate((valid) => {
        // console.log(valid)
        if (valid) {
          if (status == 0) {
            this.saveLoading = true
          } else {
            this.loadingVisible = true
          }

          let dmsMoveDetailList = this.dataSource.map(item => {
            return {
              ...item,
              oldPosition: item.orgName,
              oldOrgId: item.orgId,
              newOrgId: item.newOrgId
            }
          })
          // console.log(dmsMoveDetailList)
          let params = {
            oldOrgId: this.formInline.oldOrgId[this.formInline.oldOrgId.length - 1],
            newOrgId: this.formInline.newOrgId,
            applyStatus: status,
            applyRemark: this.formInline.applyRemark,
            applyUserName: localStorage.getItem('ziyunFactoryPortalName'),
            applyUserNo: localStorage.getItem('ziyunFactoryUserId'),
            oldBelongCompanyId: this.formInline.oldBelongCompanyId,
            newBelongCompanyId: this.formInline.newBelongCompanyId,
            newBelongCompanyName: newBelongCompanyName,
            moveInAdminName: moveInAdminName,
            moveInAdminNo: moveInAdminNo,
            moveOutAdminName: moveOutAdminName,
            moveOutAdminNo: moveOutAdminNo,
            moveOutCostCenter: this.formInline.moveOutCostCenter,
            moveOutCostCenterName: this.formInline.moveOutCostCenterName,
            ydlx: this.formInline.ydlx,
            moveInCostCenter: this.formInline.moveInCostCenter,
            moveInCostCenterName: this.formInline.moveInCostCenterName,
            moveNum: dmsMoveDetailList.length,
            dmsMoveDetailList,
            moveInAdminId: this.formInline.moveInAdminId,
            moveOutAdminId: this.formInline.moveOutAdminId
          }
          // console.log(params)
          if (!this.id) {
            this.axios({
              method: 'post',
              url: '/dms/dmsMoveApply/add',
              data: params
            }).then(res => {
              if (res.code === 200) {
                this.$emit('handleRefresh')
                this.$Message.success(({content: '新增成功！', duration: 6, closable: true}))
                this.$router.go(-1)
              } else {
                this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
              }
            }).finally(() => {
              this.changesVisible = false
              if (status == 0) {
                this.saveLoading = false
              } else {
                this.loadingVisible = false
              }
              // this.loadingVisible = false
              this.$refs.formChanges.resetFields()
            })
          } else {
            this.axios({
              method: 'post',
              url: '/dms/dmsMoveApply/update',
              data: {
                ...params,
                id: this.id
              }
            }).then(res => {
              if (res.code === 200) {
                this.$emit('handleRefresh')
                this.$Message.success(({content: '编辑成功！', duration: 6, closable: true}))
                this.$router.go(-1)
              } else {
                this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
              }
            }).finally(() => {
              this.changesVisible = false
              if (status == 0) {
                this.saveLoading = false
              } else {
                this.loadingVisible = false
              }
              // this.loadingVisible = false
              this.$refs.formChanges.resetFields()
            })
          }
        }
      })


    }
  }
}
</script>

<style scoped lang="scss">
.addModal {
  .ivu-radio-wrapper {
    color: black;
  }

  .addProject {
    .modalTitle {
      position: relative;

      span {
        font-weight: bold;
        font-size: 20px;
      }

      .label {
        display: flex;
        flex-wrap: wrap;
        padding: 0;

        .item {
          display: flex;
          margin-left: 20px;

          label {
            width: 150px;
            height: 100%;
            background: #F5F6F7;
          }

          & > div {
            width: 330px;
          }

        }

        .cycle {
          display: flex;

          /deep/.ivu-form-item-label {
            //width: 150px;
            //height: 100%;
            //background: #F5F6F7;
            padding: 0;
          }

           /deep/.ivu-form-item-content {
            border: 1px solid #EEEEEE;
            width: auto;
            display: inline-block;
            /*width: 800px;*/
            .ivu-checkbox-wrapper {
              margin-left: 30px;
            }
          }
        }
      }
    }

    .modalTitle::before {
      content: '';
      position: absolute;
      width: 3px;
      height: 20px;
      top: 5px;
      left: -10px;
      background: #187BDB;
    }

    .tab {
      display: flex;
      align-items: center;
      flex-wrap: wrap;

      .icon {
        border: 1px solid #187BDB;
        border-radius: 30px;
        padding: 5px 20px 5px 20px;
        display: flex;
        align-items: center;
        margin-left: 20px;
        color: #187BDB;

        i {
          font-size: 20px;
          font-weight: bold;
        }
      }
    }
  }
}
</style>
<style lang="scss">
.addAndDetail{
  .detail-content-layout{
    .ivu-form-item-label{
      display: block;
      width: 100%;
      text-align: left;
      padding: 10px 0 5px 0;
    }
    .ivu-form-item-content {
      display: block;
      text-align: left;

    }
    .ivu-radio-wrapper{
      color: #333333;
    }
    .item.ivu-form-item{
      margin-bottom: 0;
    }
    //.ivu-select-disabled .ivu-select-selection .ivu-select-selected-value{
    //  color: #cccccc !important;
    //}
  }

}
</style>
